﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Ole = AnalysisSystem.OleDbMethod;

namespace AnalysisSystem
{
    class SingleWellDailyProduction
    {
        /// <summary>
        /// 对单井求解计算
        /// </summary>
        /// <param name="wellName">井名</param>
        /// <returns></returns>
        public static DataTable DailyProductionCalculation(string wellName)
        {
            DataTable wellDataReturn = new DataTable();
            var rows = from row in Ole.dataTables[1].AsEnumerable()
                       where row.Field<string>("井号").Equals(wellName)
                       select row;
            wellDataReturn = rows.CopyToDataTable<DataRow>();
            //test
            //if (wellDataReturn.Rows.Count > 500)
            //{
            //    for (int i = wellDataReturn.Rows.Count - 1; i >= 500; i--)
            //    {
            //        wellDataReturn.Rows.RemoveAt(i);
            //    }
            //}
            //test
            CreateNewColumn(ref wellDataReturn);
            for (int i = 0; i < wellDataReturn.Rows.Count; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    wellDataReturn.Rows[i][j + 8] = ValueGet(i, j, ref wellDataReturn);
                }
                if (wellDataReturn.Rows[i][7].Equals(DBNull.Value))
                    wellDataReturn.Rows[i][11] = DBNull.Value;
                else wellDataReturn.Rows[i][11] = ValueCalculation(i, 0, ref wellDataReturn);
                if (wellDataReturn.Rows[i][10].Equals(DBNull.Value))
                    wellDataReturn.Rows[i][12] = DBNull.Value;
                else wellDataReturn.Rows[i][12] = ValueCalculation(i, 1, ref wellDataReturn);
            }
            return wellDataReturn;
        }
        /// <summary>
        /// 添加所需列
        /// </summary>
        /// <param name="table">表</param>
        private static void CreateNewColumn(ref DataTable table)
        {
            table.Columns.Remove("总序号");
            table.Columns.Add("累计产油（吨）");
            table.Columns.Add("累计产水（方）");
            table.Columns.Add("累计产气（万方）");
            table.Columns.Add("日水气比（方/万方）");
            table.Columns.Add("累计水气比（方/万方）");
        }

        private static double ValueGet(int row, int increment, ref DataTable table)
        {
            double value = 0;
            if (row > 0) value = Convert.ToDouble(table.Rows[row - 1][8 + increment]);
            if (table.Rows[row][5 + increment].Equals(DBNull.Value)) value += 0;
            else value += Convert.ToDouble(table.Rows[row][5 + increment]);
            return Math.Round(value, 2);
        }

        private static double ValueCalculation(int row, int type, ref DataTable table)
        {
            double value = 0;
            if (type == 0)
            {
                if (table.Rows[row][6].Equals(DBNull.Value))
                    value = 0;
                else
                    value = Convert.ToDouble(table.Rows[row][6]) / Convert.ToDouble(table.Rows[row][7]);
            }
            else
            {
                if (table.Rows[row][9].Equals(DBNull.Value))
                    value = 0;
                else
                    value = Convert.ToDouble(table.Rows[row][9]) / Convert.ToDouble(table.Rows[row][10]);
            }
            return Math.Round(value, 2);
        }

        public static List<string> ShowWellInformation(string wellName)
        {
            List<string> list = new List<string>();
            if (SingleWellSummary.singleWellSumTable.Rows.Count == 0)
                SingleWellSummary.SingleWellSum();
            var row = from p in SingleWellSummary.singleWellSumTable.AsEnumerable()
                      where p.Field<string>("井号").Equals(wellName)
                      select p;
            DataTable table = row.CopyToDataTable<DataRow>();
            string timeRate = table.Rows[0]["开井时率（%）"].ToString();
            list.Add(string.Format("开井时率%：{0}", timeRate));
            list.Add(string.Format("平均日产气（万方/天）: {0}", table.Rows[0]["平均日产气（万方/天）"].ToString()));
            list.Add(string.Format("累计产气（万方）: {0}", table.Rows[0]["累计产气（万方）"].ToString()));
            list.Add(string.Format("平均日产水（方/天）: {0}", table.Rows[0]["平均日产水（方/天）"].ToString()));
            list.Add(string.Format("累计产水（方）: {0}", table.Rows[0]["累计产水（方）"].ToString()));
            list.Add(string.Format("累计产油（吨）: {0}", table.Rows[0]["累计产油（吨）"].ToString()));
            return list;
        }
    }
}
